package com.platform.core.web.controller;

import com.google.common.collect.Maps;
import com.platform.core.service.NcloudSysInterfaceBiz;
import com.platform.dto.InterfaceDto;
import com.platform.dto.InterfaceTypeDto;
import com.platform.analysis.enums.ModelDataType;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.comm.web.results.JsonResult;
import com.platform.comm.web.results.PageListJsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Description: 接口管理
 *
 * @author Coder
 */
@RestController
@RequestMapping(name = "接口管理", path = "/management/interface")
public class InterfaceController extends BaseController{

    private final NcloudSysInterfaceBiz ncloudSysInterfaceBiz;

    @Autowired
    public InterfaceController(NcloudSysInterfaceBiz ncloudSysInterfaceBiz) {
        this.ncloudSysInterfaceBiz = ncloudSysInterfaceBiz;
    }

    /**
     * 分页查询接口数据
     */
    @PostMapping(name = "查询接口列表", path = "/search")
    public PageListJsonResult<InterfaceDto> search(@RequestBody Map<String, String> map) {
        Page<InterfaceDto> entities = ncloudSysInterfaceBiz.search(map, getPageable(map));
        return new PageListJsonResult<>(entities);
    }

    /**
     * 分页查询已发布接口列表
     */
    @PostMapping(name = "分页查询已发布接口列表", path = "/searchPublish")
    public PageListJsonResult<InterfaceTypeDto> searchPublish() {
        List<InterfaceTypeDto> entities = ncloudSysInterfaceBiz.searchPublish("0");
        return new PageListJsonResult<>(entities);
    }

    /**
    * 查询接口明细数据
    */
    @PostMapping(name = "查询接口明细", path = "/detail")
    public DataJsonResult<InterfaceDto> detail(@RequestBody Map<String,String> param) {
        return DataJsonResult.succeed(ncloudSysInterfaceBiz.detail(param.get("id")));
    }

    /**
    * 新增接口
    */
    @PostMapping(name = "新增接口", path = "/create")
    public JsonResult create(@RequestBody InterfaceDto req) {
        ncloudSysInterfaceBiz.create(req);
        return JsonResult.success();
    }

    /**
    * 修改接口数据
    */
    @PostMapping(name = "修改接口", path = "/modify")
    public JsonResult modifyNcloudSysInterface(@RequestBody InterfaceDto req) {
        ncloudSysInterfaceBiz.modify(req);
        return JsonResult.success();
    }

    /**
    * 删除接口
    */
    @PostMapping(name = "删除接口", path = "/remove")
    public JsonResult remove(@RequestBody Map<String,String> param) {
        ncloudSysInterfaceBiz.remove(param.get("id"));
        return JsonResult.success();
    }

    /**
     * 发布接口
     */
    @PostMapping(name = "发布接口", path = "/publish")
    public JsonResult publish(@RequestBody Map<String,String> param) {
        ncloudSysInterfaceBiz.publish(param.get("id"));
        return JsonResult.success();
    }

    @PostMapping(name = "获取数据类型",path = "/getDataType")
    public DataJsonResult<Map<String,String>> getDataType(){
        Map<String, String> dataTypeMap = Maps.newHashMap();
        Arrays.stream(ModelDataType.values()).forEach(dataType -> dataTypeMap.put(dataType.key(),dataType.value()));
        return DataJsonResult.succeed(dataTypeMap);
    }

    @PostMapping(name = "克隆接口",path = "/clone")
    public JsonResult clone(@RequestBody Map<String,String> param) throws CloneNotSupportedException {
        ncloudSysInterfaceBiz.clone(param);
        return JsonResult.success();
    }

    @PostMapping(name = "设置接口已被使用",path = "/setIsUsed")
    public JsonResult setIsUsed(@RequestParam String id) {
        ncloudSysInterfaceBiz.setIsUsed(id);
        return JsonResult.success();
    }

    /**
     * (对外)查询接口明细数据
     */
    @PostMapping(name = "(对外)查询接口明细数据", path = "api/detail")
    public InterfaceDto apiDetail(@RequestParam String id) {
        return ncloudSysInterfaceBiz.detail(id);
    }
}
